// The CameraVideoPageController is a class that controls the camera 
// video page.  This class provides a some useful methods you will
// need to call:
//     cameraVideoPage.displayMessage(message, timeout):
//         Causes a short message string to be displayed on the
//         page for a brief period.  Useful for showing quick
//         notifications to the user.  message is a plain string.
//         timeout is option and denotes the length of time in msec
//         to show the message for.
//     cameraVideoPage.setHeadsUpDisplayHTML(html):
//         This will set or update the heads-up-display with the
//         text given in the html argument.  Usually this should 
//         just be a string with text and line breaks (<br />).

// Initialise the camera video page and callback to our 
// cameraVideoPageInitialised() function when ready.
var cameraVideoPage = new CameraVideoPageController(
        cameraVideoPageInitialised);

// You may need to create variables to store state.

// the height of the phone.
var height;
// Orientation sense
var alpha;
var gamma;
var beta;
// the tilt to apex
var apexAlpha = 0;
var apexBeta = 0;
var apexGamma = 0;
var apexAbsolute = 0;
// the tilt of base
var baseAlpha = 0;
var baseBeta = 0;
var baseGamma = 0;
var baseAbsolute = 0;
// This function will be called when the camera video page
// is initialised and ready to be used.

if(window.DeviceOrientationEvent)
    {
        window.addEventListener('deviceorientation', cameraVideoPageInitialised)
    }
function cameraVideoPageInitialised(e)
{
    // Step 1: Check for and initialise deviceMotion
    if(e.alpha == null && e.beta == null && e.gamma==null)
    {
        cameraVideoPage.setHeadsUpDisplayHTML("Team1");
        return;
    }
    alpha = Number(e.alpha).toFixed(2);
    beta = Number(e.beta).toFixed(2);
    gamma = Number(e.gamma).toFixed(2);

    cameraVideoPage.setHeadsUpDisplayHTML("Team1</br>alpha:"+Alpha+"</br>"+"beta:"+Beta+"</br>"+"gamma:"+Gamma);
}
    
// This function is called by a button to set the height of phone from the
// ground, in metres.
function setCameraHeightValue()
{
    height = prompt("Please enter the height of the phone.");
    cameraVideoPage.displayMessage("Your camera height is "+height.toString(), 1000)
    // Step 2: Set camera height
    // check if input is a number and is positive
    // display on screen using the displayMessage method
}
    
// This function is called by a button to set the angle to the base of
// the object being measured.  It uses the current smoothed tilt angle.
function setBaseTiltAngle()
{
    // Step 3: Record tilt angle
    // display on screen using the displayMessage method
//    window.addEventListener("deviceorientation", function(event){
//        baseAlpha = event.alpha;
//        baseBeta = event.beta;
//        baseGamma = event.gamma;
//        baseAbsolute = event.absolute;
//        cameraVideoPage.displayMessage("Your beta is "+baseBeta.toString(), 1000);
//        cameraVideoPage.displayMessage("alpha:"+baseAlpha.toString()+"</br>");
//    });
        window.addEventListener('deviceorientation', function(event) {
        baseAlpha = event.alpha;
        baseBeta = event.beta;
        baseGamma = event.gamma;
        baseAbsolute = event.absolute;
        ;});
        cameraVideoPage.displayMessage("Your beta is "+baseBeta.toString(), 1000)
}

// This function is called by a button to set the angle to the apex of
// the object being measured.  It uses the current smoothed tilt angle.
function setApexTiltAngle()
{
    // Step 4: Record tilt angle 
    // display on screen using the displayMessage method
    var count = 0;
    window.addEventListener("deviceorientation", function(event){
        while(count!=5){
        apexAlpha += event.alpha;
        apexBeta += event.beta;
        apexGamma += event.gamma;
        apexAbsolute += event.absolute;
        count +=1;
        }
    });
    apexBeta /= 5;
    cameraVideoPage.displayMessage("Beta:"+apexBeta.toString()+"</br>")
//    showHeight();
}

// You may need to write several other functions.
function showHeight(event){
    var distance = height * Math.tan(baseBeta);
    var angle = baseBeta - apexBeta;
    var top = angle * Math.tan(angle) + height;
    cameraVideoPage.displayMessage("The buliding height is "+top.toString(), 1000)
}